#!/bin/sh
# definitions 
mysql="mysql -uroot -pijiapu.sql.admin -h127.0.0.1 qin"
#mysql="mysql -uqin -p123456 -h127.0.0.1 qin"
head='<?xml version="1.0" encoding="UTF-8"?> 
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">'
tail='</urlset>'
max_user_id_file="site.max-user-id"
public="site.public.xml"
surnames="site.surnames.xml"
sitemaps="sitemap.xml"
users_per_page=2000

function update_users_map
{
    if [ -f $max_user_id_file ];then
	max_user_id=`cat $max_user_id_file`
    else
	max_user_id=0
    fi

    start_user_id=$(($max_user_id-$max_user_id%$users_per_page))
    end_user_id=$(($start_user_id+$users_per_page+1))
    page=$(($max_user_id/$users_per_page + 1))
    users="site.users.${page}.xml"
# users tree/index, home and album

    echo $head > $users
    $mysql -e "set names utf8; select id,ts_created from users where state='init' and id > $start_user_id and id < $end_user_id;" | awk 'FNR>1{print "<url><loc>http://www.ijiapu.org/users/home?userid="$1"</loc><priority>0.8</priority><lastmod>"$2"T"$3"+00:00</lastmod><changefreq>Weekly</changefreq></url>\n<url><loc>http://www.ijiapu.org/users/album?userid="$1"</loc><priority>0.6</priority><lastmod>"$2"T"$3"+00:00</lastmod><changefreq>Weekly</changefreq></url>\n<url><loc>http://www.ijiapu.org/tree/index?userid="$1"</loc><priority>1.0</priority><lastmod>"$2"T"$3"+00:00</lastmod><changefreq>Weekly</changefreq></url>\n"}' >> $users
    echo $tail >> $users

    $mysql -e "select id from users where state='init' and id > $start_user_id and id < $end_user_id;" | sed -n '$p' > $max_user_id_file

    # update last modified tag 
    today=`date +"%Y-%m-%d"`
    if [ -f $sitemaps ]; then
	grep -q $users $sitemaps 
	if [ $? -eq 0 ]; then
	    #sed -r 's#('$users'</loc> <lastmod>)[0-9-]*(</lastmod>)#\1'$today'\2#' $sitemaps
	    sed -i -r '/'$users'/s/[0-9-]{9,}/'$today'/' $sitemaps
	else
	    sed -i '/site.users./a<sitemap><loc>http://www.ijiapu.org/sitemaps/'${users}'</loc> <lastmod>'${today}'</lastmod></sitemap>' $sitemaps
	fi
    fi
}

# create public links sitemap
function update_public_map
{
    today=`date +"%Y-%m-%dT%H:%M:%S+00:00"`
    echo $head > $public
    for link in "" "/index/guide" "/index/links" "/index/copyright" "/users/register" "/users/login" "/users/show_photo_wall" "/users/get_more_latest"
    do
	echo "<url><loc>http://www.ijiapu.org${link}</loc><priority>0.5</priority><lastmod>${today}</lastmod><changefreq>Weekly</changefreq></url>" >> $public
    done
    echo $tail >> $public
}

# create surnames sitemap
function create_surnames_map
{
    today=`date +"%Y-%m-%dT%H:%M:%S+00:00"`
    echo $head > $surnames
    echo "<url><loc>http://www.ijiapu.org/surnames/index</loc><priority>0.7</priority><lastmod>${today}</lastmod><changefreq>Weekly</changefreq></url>" >> $surnames
    awk -v today=$today '{print "<url><loc>http://www.ijiapu.org/surnames/show?surname="$1"</loc><priority>0.9</priority><lastmod>"today"</lastmod><changefreq>Weekly</changefreq></url>"}' "../Public/surnames.txt" >> $surnames
    echo $tail >> $surnames
}

function create_index
{
    today=`date +"%Y-%m-%d"`
    echo "<?xml version='1.0' encoding='UTF-8'?>
<sitemapindex xmlns='http://www.sitemaps.org/schemas/sitemap/0.9'>
<sitemap><loc>http://www.ijiapu.org/sitemaps/${public}</loc> <lastmod>${today}</lastmod></sitemap>
<sitemap><loc>http://www.ijiapu.org/sitemaps/${surnames}</loc> <lastmod>${today}</lastmod></sitemap>" > $sitemaps
    
    for file in site.users.*.xml
    do 
	echo "<sitemap><loc>http://www.ijiapu.org/sitemaps/${file}</loc><lastmod>${today}</lastmod></sitemap>" >> $sitemaps
    done

    echo "</sitemapindex>" >> $sitemaps

    users="site.users.${page}.xml"
}

case $1 in 
    --update-public)
	update_public_map
	;;
    --createusers)
	update_users_map
	;;
    --createsurnames)
	create_surnames_map
	;;
    --createall)
	update_public_map
	update_users_map
	create_surnames_map
	create_index
	;;
    *)
	printf "Usage: $0 --createusers\n\t--createhome\n\t--createindex\n\t--createsurnames\n\t--createall\n";
	exit	
	;;
esac

